﻿using System;
using UnityEngine;
using LitJson;
using System.Collections.Generic;
using MTUnity.Utils;
using UnityEngine.SocialPlatforms;

namespace Script.UI
{
    public class UIBase<T> : UIBase where T : class
    {
        protected T data
        {
            get { return _data as T; }
        }
    }

    public enum Layer
    {
        Low,
        Normal,
        High,
        Pop,
    }
    
    public class UIBase : MonoBehaviour
    {
        public Layer Layer = Layer.Normal;
        protected object _data;

        protected void Awake()
        {
            OnAwake();
        }

        public void SetParentWithLayer()
        {
            var obj = GameObject.Find("UIRoot/" + Layer.ToString());
            transform.SetParent(obj.transform);
            transform.localPosition = Vector3.zero;
            transform.localScale = Vector3.one;
        }

        protected virtual void OnAwake()
        {
            
        }

        public void Refresh(object data)
        {
            _data = data;
            OnRefresh();
        }

        protected virtual void OnRefresh()
        {
            
        }
    }


    public delegate void UpdateDel(JsonData updateData);
    public delegate JsonData GatherDel();
    public interface IUIUpdateInterface
    {

        Dictionary<string , UpdateDel> RegisterProcessorMethod();
        Dictionary<string, IUIUpdateInterface> RegisterProcessorObj();
        Dictionary<string, GatherDel> RegisterGatherMethod();
        void DataProcessor(JsonData updateData);
    }

    public class UIUpdateBase : MonoBehaviour, IUIUpdateInterface
    {
        Dictionary<string, UpdateDel> m_dicEvent2Del;
        Dictionary<string, IUIUpdateInterface> m_dicEvent2Obj;
        Dictionary<string, GatherDel> m_dicDataGatherDel;

        private bool m_bInit = false;

        protected void Awake()
        {
            Load();

            OnAwake();
        }

        private void Load()
        {
            if (m_bInit)
            {
                return;
            }

            m_dicEvent2Del = RegisterProcessorMethod();
            if (m_dicEvent2Del == null)
            {
                m_dicEvent2Del = new Dictionary<string, UpdateDel>();
            }
            m_dicEvent2Obj = RegisterProcessorObj();
            if (m_dicEvent2Obj == null)
            {
                m_dicEvent2Obj = new Dictionary<string, IUIUpdateInterface>();
            }

            m_dicDataGatherDel = RegisterGatherMethod();
            if (m_dicDataGatherDel == null)
            {
                m_dicDataGatherDel = new Dictionary<string, GatherDel>();
            }

            m_bInit = true;
        }
        public virtual void OnAwake()
        {
            
        }

        public virtual Dictionary<string ,UpdateDel>  RegisterProcessorMethod()
        {
            return new Dictionary<string, UpdateDel>();
        }

        public virtual Dictionary<string , IUIUpdateInterface> RegisterProcessorObj()
        {
            return new Dictionary<string, IUIUpdateInterface>();
        }

        public virtual Dictionary<string, GatherDel> RegisterGatherMethod()
        {
            return new Dictionary<string, GatherDel>();
        }

        public void DataProcessor(JsonData updateData)
        {
            Load();
            if (updateData == null)
            {
                return;
            }

            foreach (var item in m_dicEvent2Obj)
            {
                if (updateData.ContainsKey(item.Key))
                {
                    item.Value.DataProcessor(updateData[item.Key]);
                }
            }

            foreach(var item in m_dicEvent2Del)
            {
                if (updateData.ContainsKey(item.Key))
                {
                    item.Value.Invoke(updateData[item.Key]);
                }
            }
        }

        public JsonData DataGather(List<string> listGatherKey)
        {

            Load();

            JsonData gatherDataRoot = new JsonData();
            gatherDataRoot.SetJsonType(JsonType.Object);
            foreach(var item in listGatherKey)
            {
                if (m_dicDataGatherDel.ContainsKey(item))
                {
                    string strDayFieldName = JsonFildName.GetDadFieldName(item);
                    if (strDayFieldName != "None")
                    {
                        JsonData parentObj = null;
                        if (!gatherDataRoot.ContainsKey(strDayFieldName))
                        {
                            parentObj = new JsonData();
                            parentObj.SetJsonType(JsonType.Object);
                            gatherDataRoot[strDayFieldName] = parentObj;
                        }
                        else
                        {
                            parentObj = gatherDataRoot[strDayFieldName];
                        }
                        JsonData gatherData = m_dicDataGatherDel[item].Invoke();
                        JsonUtils.GenerateJsonDataWithFieldName(ref parentObj, item, gatherData);
                    }
                    
                }
            }
            return gatherDataRoot;
        }
    }
}